package com.leapp.sk.util

import cc.vv.lkbasecomponent.util.LKDateTimeUtil.getStrTime
import java.util.*

/**
 * @author  chaohaipeng  on 2018/7/17.
 */
class SKTimeUtil {
    companion object {
        val SECONDS_IN_DAY = 60 * 60 * 24
        val MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY
        /**
         * 获取两个时间之间的间隔
         *
         * @param time
         * @param _time
         * @return
         */
        fun getTimeInterval(time: Long, _time: Long): Int {
            val longIntervalTime = Math.abs(time - _time)
            return (longIntervalTime / 1000 / 60).toInt()
        }


        /**
         * 获取时间字符串，今天为时分显示，非今天为日期显示
         *
         * @param millis
         * @return
         */
        fun getDayOrHours(millis: Long): String {
            var strTime = ""
            val data = Date()
            val nowTime = data.time
            val sameDayOfMillis = isSameDayOfMillis(nowTime, millis)
            //"yyyy年MM月dd日HH时mm分ss秒"
            if (sameDayOfMillis) {
                strTime = getStrTime(millis, "HH:mm")
            } else {
                strTime = getStrTime(millis, "MM/dd HH:mm")
            }
            return strTime
        }

        /**
         * 判断是否为一天
         *
         * @param ms1
         * @param ms2
         * @return
         */
        fun isSameDayOfMillis(ms1: Long, ms2: Long): Boolean {
            val interval = ms1 - ms2
            return (interval < MILLIS_IN_DAY
                    && interval > -1L * MILLIS_IN_DAY
                    && toDay(ms1) == toDay(ms2))
        }

        private fun toDay(millis: Long): Long {
            return (millis + TimeZone.getDefault().getOffset(millis)) / MILLIS_IN_DAY
        }
    }

}